# eric7.UI.PythonAstViewer

Module implementing a widget to visualize the Python AST for some Python sources.

### Global Attributes

None

### Classes

PythonAstViewer | Class implementing a widget to visualize the Python AST for some Python sources.

### Functions

None

## PythonAstViewer

Class implementing a widget to visualize the Python AST for some Python sources.

### Derived from

QWidget

### Class Attributes

EndIndexRole
EndLineRole
StartIndexRole
StartLineRole

### Class Methods

None

### Methods

PythonAstViewer | Constructor
__astItemClicked | Private slot handling a user click on an AST node item.
__astViewerStateChanged | Private slot to toggle the display of the AST viewer.
__clearSelection | Private method to clear all selected items.
__createErrorItem | Private method to create a top level error item.
__editorChanged | Private slot to handle a change of the current editor.
__editorDoubleClicked | Private slot to handle a mouse button double click in the editor.
__editorLanguageChanged | Private slot to handle a change of the editor language.
__editorSaved | Private slot to reload the AST after the connected editor was saved.
__findClosestContainingItem | Private method to search for the tree item that contains a range closest.
__findClosestContainingNode | Private method to search for the AST node that contains a range closest.
__grabFocus | Private method to grab the input focus.
__lastEditorClosed | Private slot to handle the last editor closed signal of the view manager.
__loadAST | Private method to generate the AST from the source of the current editor and visualize it.
__markTextRanges | Private method to modify the AST nodes with end_lineno and end_col_offset information.
__populateNode | Private method to populate the tree view with a node.
__rangeContainsSmaller | Private method to check, if second is contained in first.
__rangeContainsSmallerOrEqual | Private method to check, if second is contained in or equal to first.
__resizeColumns | Private method to resize the columns to suitable values.
__selectItemForEditorSelection | Private slot to select the item corresponding to an editor selection.
__shutdown | Private method to perform shutdown actions.
hide | Public slot to hide the AST viewer.
preferencesChanged | Public slot handling changes of the AST viewer settings.
resizeEvent | Protected method to handle resize events.
show | Public slot to show the AST viewer.

### Static Methods

None

#### PythonAstViewer (Constructor)

PythonAstViewer(viewmanager, ui)

Constructor

viewmanager (ViewManager)
  reference to the viewmanager object
ui (QWidget)
  reference to the main ui widget

#### PythonAstViewer.__astItemClicked

__astItemClicked(itm, _column)

Private slot handling a user click on an AST node item.

itm (QTreeWidgetItem)
  reference to the clicked item
_column (int)
  column number of the click (unused)

#### PythonAstViewer.__astViewerStateChanged

__astViewerStateChanged(on)

Private slot to toggle the display of the AST viewer.

on (bool)
  flag indicating to show the AST

#### PythonAstViewer.__clearSelection

__clearSelection()

Private method to clear all selected items.

#### PythonAstViewer.__createErrorItem

__createErrorItem(error)

Private method to create a top level error item.

error (str)
  error message

Return:
  generated item

Return Type:
  QTreeWidgetItem

#### PythonAstViewer.__editorChanged

__editorChanged(editor)

Private slot to handle a change of the current editor.

editor (Editor)
  reference to the current editor

#### PythonAstViewer.__editorDoubleClicked

__editorDoubleClicked(editor, _pos, buttons)

Private slot to handle a mouse button double click in the editor.

editor (Editor)
  reference to the editor, that emitted the signal
_pos (QPoint)
  position of the double click (unused)
buttons (Qt.MouseButton)
  mouse buttons that were double clicked

#### PythonAstViewer.__editorLanguageChanged

__editorLanguageChanged(editor)

Private slot to handle a change of the editor language.

editor (Editor)
  reference to the editor which changed language

#### PythonAstViewer.__editorSaved

__editorSaved(editor)

Private slot to reload the AST after the connected editor was saved.

editor (Editor)
  reference to the editor that performed a save action

#### PythonAstViewer.__findClosestContainingItem

__findClosestContainingItem(itm, textRange)

Private method to search for the tree item that contains a range closest.

itm (QTreeWidgetItem)
  tree item to start searching at
textRange (tuple of (int, int, int, int))
  tuple giving the start and end positions

Return:
  best matching tree item

Return Type:
  QTreeWidgetItem

#### PythonAstViewer.__findClosestContainingNode

__findClosestContainingNode(node, textRange)

Private method to search for the AST node that contains a range closest.

node (ast.AST)
  AST node to start searching at
textRange (tuple of (int, int, int, int))
  tuple giving the start and end positions

Return:
  best matching node

Return Type:
  ast.AST

#### PythonAstViewer.__grabFocus

__grabFocus()

Private method to grab the input focus.

#### PythonAstViewer.__lastEditorClosed

__lastEditorClosed()

Private slot to handle the last editor closed signal of the view manager.

#### PythonAstViewer.__loadAST

__loadAST()

Private method to generate the AST from the source of the current editor and visualize it.

#### PythonAstViewer.__markTextRanges

__markTextRanges(tree, source)

Private method to modify the AST nodes with end_lineno and end_col_offset information.

Note: The modifications are only done for nodes containing lineno and col_offset attributes.

tree (ast.AST)
  reference to the AST to be modified
source (str)
  source code the AST was created from

#### PythonAstViewer.__populateNode

__populateNode(name, nodeOrFields, parent)

Private method to populate the tree view with a node.

name (str)
  name of the node
nodeOrFields (ast.AST or list)
  reference to the node or a list node fields
parent (QTreeWidget or QTreeWidgetItem)
  reference to the parent item

#### PythonAstViewer.__rangeContainsSmaller

__rangeContainsSmaller(first, second)

Private method to check, if second is contained in first.

first (tuple of (int, int, int, int))
  text range to check against
second (tuple of (int, int, int, int))
  text range to check for

Return:
  flag indicating second is contained in first

Return Type:
  bool

#### PythonAstViewer.__rangeContainsSmallerOrEqual

__rangeContainsSmallerOrEqual(first, second)

Private method to check, if second is contained in or equal to first.

first (tuple of (int, int, int, int))
  text range to check against
second (tuple of (int, int, int, int))
  text range to check for

Return:
  flag indicating second is contained in or equal to first

Return Type:
  bool

#### PythonAstViewer.__resizeColumns

__resizeColumns()

Private method to resize the columns to suitable values.

#### PythonAstViewer.__selectItemForEditorSelection

__selectItemForEditorSelection()

Private slot to select the item corresponding to an editor selection.

#### PythonAstViewer.__shutdown

__shutdown()

Private method to perform shutdown actions.

#### PythonAstViewer.hide

hide()

Public slot to hide the AST viewer.

#### PythonAstViewer.preferencesChanged

preferencesChanged()

Public slot handling changes of the AST viewer settings.

#### PythonAstViewer.resizeEvent

resizeEvent(_evt)

Protected method to handle resize events.

_evt (QResizeEvent)
  resize event (unused)

#### PythonAstViewer.show

show()

Public slot to show the AST viewer.

Up